﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Xml;
using System.Drawing;
using System.Threading;

namespace ImageEditor
{
    public class MapPanel : Panel
    {
        private ArrayList _statesManager = new ArrayList();
        private bool _initialized = false;
        private Map _map = new Map();
        private TitlePanel _titlePanel;

        public MapPanel()
        {
            _titlePanel = new TitlePanel(_map.CurrentPeriod.Subtitle);
            InitializePanel();
        }

        private void InitializePanel()
        {
            this.Location = new Point(20, 20);
            this.Size = new Size(1200, 675);
            this.BackColor = Color.Black;
            this.BorderStyle = BorderStyle.FixedSingle;
            this.MouseClick += new MouseEventHandler(MapPanel_MouseClick);
            this.Controls.Add(new MapKey());
            this.Controls.Add(new MapController(this));
            this.Controls.Add(_titlePanel);
        }

        private void InitializeMap()
        {
            if (_initialized)
                return;

            Graphics g = this.CreateGraphics();
            foreach (State state in _map.CurrentPeriod.States)
            {
                g.DrawImageUnscaled(state.StateImage, state.Location);
                _statesManager.Add(state);
            }
            g.Dispose();
            _initialized = true;
        }

        public void UpdateMap(String direction)
        {
            TimePeriod period;
            Graphics g = this.CreateGraphics();
            if (direction.Equals("LEFT")) { period = _map.PreviousPeriod; } else { period = _map.NextPeriod; }
            foreach (State state in period.States)
            {
                g.DrawImageUnscaled(state.StateImage, state.Location);
            }
            UpdateTitle(period.Subtitle);
            g.Dispose();
        }

        public void PlayMap()
        {
            Graphics g = this.CreateGraphics();
            foreach(TimePeriod period in _map.TimePeriods)
            {
                foreach (State state in period.States)
                {
                    g.DrawImageUnscaled(state.StateImage, state.Location);
                }
                UpdateTitle(period.Subtitle);
                Thread.Sleep(250);
            }
            _map.CurrentPeriod = _map.FinalPeriod;
            g.Dispose();
        }

        private void UpdateTitle(String text, Graphics g)
        {
            g.DrawString(text, new Font(FontFamily.GenericSerif, 12.0F), new SolidBrush(Color.Black), new PointF(600, 30));
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            InitializeMap();
        }

        protected void MapPanel_MouseClick(object sender, MouseEventArgs e)
        {
            foreach (State state in _statesManager)
            {
                if (state.Rectangle.Contains(e.Location))
                {
                    Console.WriteLine(state.Name);
                    break;
                }
            }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            if (e.KeyChar.Equals(Keys.Left))
            {
                UpdateMap("LEFT");
            }
            else if (e.KeyChar.Equals(Keys.Right))
            {
                UpdateMap("RIGHT");
            }
        }

        private void UpdateTitle(String text)
        {
            _titlePanel.UpdateText(text);
        }
    }
}
